angular.module('Controller')

.controller('QgyContractDetailCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    console.log($rootScope.pageType)
    if $rootScope.pageType=='contract-detail'
#接受数据:{houseId,contractId,house,contract}
        data=angular.copy($rootScope.pageParams)
        $scope.contractId=data.contractId
        $scope.contract=data.contract
        $rootScope.pageParams=undefined
        $rootScope.pageType=undefined
    $scope.housePayTypeList=[
        {value: 1, name: '押一付三'},
        {value: 2, name: '押一付一'},
        {value: 3, name: '押一付一/押一付三'},
        {value: 4, name: '租赁贷'}
        {value: 5, name: '押一付三/租赁贷'}
        {value: 6, name: '押一付一/租赁贷'}
        {value: 7, name: '押一付三/押一付一/租赁贷'}
    ]
    loadData=()->
        loadContractById($scope.contractId)
    loadContractById=(contractId)->
        $http.get("/contract/#{contractId}/detail")
        .success((resp)->
            if resp.code==0
                $scope.contract=resp.message
                $scope.contract.cotenants=[] if $scope.contract.cotenants==undefined
        )
    loadData()
    $scope.sexList=commonConstant.qgySexList()
    _.extend $scope,
        openCotenant:(type,item)->
            modal=showCotenantModal(type,item)
            modal.result.then((modalData)->
                if modalData && type!='view'
                    item=modalData
            )
            ''
        doRelet:()->
            $rootScope.pageType='relet-check-in'
            $rootScope.pageParams=angular.copy({house:$scope.house,contract:$scope.contract})
            menuManage.goState('relet-check-in',true)
        doOffCheckIn:()->
            $rootScope.pageType='offline-check'
            $rootScope.pageParams=angular.copy($scope.house)
            menuManage.goState('offline-check-in',true)
        doOnCheckIn:()->
            $rootScope.pageType='online-check'
            $rootScope.pageParams=angular.copy($scope.house)
            menuManage.goState('online-check-in',true)
        doOffLease:()->
            $rootScope.pageType='off-lease-confirm'
            data={
                houseId:$scope.houseId
                house:$scope.house
                contractId:$scope.contractId
                contract:$scope.contract
            }
            $rootScope.pageParams=data
            menuManage.goState('off-lease-confirm',true)
        changeHouse:()->
            $rootScope.pageType='change-house-check-in'
            $rootScope.pageParams=angular.copy({house:$scope.house,contract:$scope.contract})
            menuManage.goState('change-house-check-in',true)
        payLease:()->
            $http.get('/bill',params:{page:0,size:999,type:1,contractId:$scope.contract.id,status:1})
            .success((resp)->
                if resp.code==0
                    if resp.message.content.length>0 and resp.message.content[0].status==1
                        $scope.recentLeaseBill=resp.message.content[0]
                        showBillModal('',angular.copy($scope.recentLeaseBill))
                    else
                        wfAlert.toastInfo('无未支付的房租账单')
                        return
                else
                    wfAlert.toastError(resp.message)
            )
        doEarnest:()->
            showEarnestModal('edit',angular.copy($scope.house))
        printContract:()->
            cnt=$scope.contract
            if cnt and cnt.contractPdf
                window.open(cnt.contractPdf, '_blank')
            else
                wfAlert.toastError('合同pdf文件未生成')
            ''
        selectTab:  (event, tabID)->
            $('#contractTab .active').removeClass('active')
            $('#' + tabID).addClass('active')
            $('#contractTabContent .active').removeClass('active')
            $('#' + tabID + 'Pane').addClass('active')
            return
    showCotenantModal=(type,data)->
        modal = $uibModal.open({
            animation: true,
            size:'lg',
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyCotenantModalCtrl',
            templateUrl: 'templates/business-manage/cotenant-modal/cotenant-modal.html',
            resolve: {
                items:  ()->{type:type,data:data}
            }
        })
    showBillModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyRentPayModalCtrl',
            templateUrl: 'templates/business-manage/rent-pay-modal/rent-pay-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            ''
        )
    showEarnestModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            size:'lg',
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyPayEarnestModalCtrl',
            templateUrl: 'templates/house-manage/pay-earnest/pay-earnest-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            ''
        )
)